package com.stox.indicator;

import java.util.List;

import lombok.Data;

import org.springframework.stereotype.Component;

import com.stox.dto.Bar;
import com.stox.dto.BarValue;
import com.stox.indicator.Stochastic.Configuration;

@Component
public class Stochastic implements Occilator<Configuration> {

	@Data
	public static class Configuration{
		private int span = 20;
		private BarValue barValue = BarValue.CLOSE;
	}

	@Override
	public Configuration createDefaultConfiguration() {
		return new Configuration();
	}

	@Override
	public Double getValue(Configuration configuration, List<Bar> bars,int index) {
		int size = bars.size();
		if(index > size - 1 - configuration.getSpan()) return null;
		double highest = Double.MIN_VALUE;
		double lowest = Double.MAX_VALUE;
		BarValue barValue = configuration.getBarValue();
		for(int i = index; i < index + configuration.getSpan(); i++){
			double value = barValue.get(bars.get(i));
			if(highest < value) highest = value;
			if(lowest > value) lowest = value;
		}
		double value = barValue.get(bars.get(index));
		if(highest == lowest) return null;
		return (value - lowest)*100/(highest - lowest);
	}

	@Override
	public Double[] getValues(Configuration configuration, List<Bar> bars) {
		Double[] result = new Double[bars.size()];
		for(int i = 0; i < bars.size(); i++){
			result[i] = getValue(configuration, bars, i);
		}
		return result;
	}
	
	@Override
	public Double[] getValues(Configuration configuration, Double[] values){
		throw new UnsupportedOperationException();
	}

	@Override
	public Double getHigh() {
		return 100d;
	}

	@Override
	public Double getLow() {
		return 0d;
	}

	private Double[] levels = {10d,90d};
	
	@Override
	public Double[] getLevels() {
		return levels;
	}
	
}
